package ObjectOrientedTest1;

import java.util.Random;

public class Role {
    //名字
    private String name;
    //血量
    private int blood;


    public Role() {
    }

    public Role(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return blood
     */
    public int getBlood() {
        return blood;
    }

    /**
     * 设置
     * @param blood
     */
    public void setBlood(int blood) {
        this.blood = blood;
    }

    public String toString() {
        return "Role{name = " + name + ", blood = " + blood + "}";
    }

    //定义一个方法攻击别人
    public void attack(Role role){
        //随机造成20以内的伤害
        Random r=new Random();
        int hart=r.nextInt(20)+1;
        //扣除血量
        int remainBlood=role.getBlood()-hart;
        //对剩余血量做验证
        remainBlood=remainBlood<0?0:remainBlood;
        //修改被打的人的血量
        role.setBlood(remainBlood);
        //结果
        System.out.println(this.name+"用拳头打了一下"+role.getName()+",造成"+hart+"伤害,"+role.getName()+"还剩"+role.getBlood()+"血");
    }


}
